<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/22
 * Time: 10:40
 */

namespace app\api\controller\v2;


use app\api\lib\exception\CouponException;
use app\api\lib\exception\MissException;
use app\api\lib\exception\SuccessMessage;
use app\api\service\TokenService;
use app\api\validate\CouponReceipt;
use app\api\model\Activity as ActivityModel;
use app\api\model\CouponTickets as CouponTicketsModel;
use app\api\model\CouponCategory as CouponCategoryModel;


class CouponReceiptController extends BaseController
{
    //活动状态详情
    //1 活动尚未开始
    //2 活动进行中
    //3 活动已经结束
    public function index()
    {
        (new CouponReceipt())->goCheck();
        $params = $this->request->param();
        $activity = ActivityModel::find($params['activity_id']);
        if($activity->is_ongoing == 0){
            throw new CouponException(['msg'=>'亲当前活动已结束!']);
            return;
        }
        if($activity->coupon_limit != 0){
            if($activity->coupon_now > $activity->coupon_limit){
                throw new CouponException(['msg'=>'亲,活动名额已满,下次早些来哦!']);
                return;
            }
        }
        if(($activity->getData('is_have_coupon')) == 0){
            throw new CouponException(['msg'=>'当前活动没有优惠券!']);
            return;
        }
        $user_id = TokenService::getCurrentUid();
        $isJudgeTag = CouponTicketsModel::where(['user_id'=>$user_id,'activity_id'=>$params['activity_id']])->find();
        if(!empty($isJudgeTag)){
            throw new CouponException(['msg'=>'亲,您已参加过该活动!']);
            return;
        }
        if($this->checkActivity($activity)){
            $ticket = [];
            $ticket['user_id'] = $user_id;
            $ticket['sequence_code'] = $this->makeSequenceCode();
            $ticket['activity_id'] = $activity->getData('id');
            $ticket['is_use'] = 0;
            $ticket['cate_id'] = $activity->is_have_coupon;
            $ticket['coupon_category_id'] = 0;
            if(empty($params['coupon_id'])){
                throw new CouponException(['msg'=>'数据异常!稍后再试!']);
            }
            $ticket['coupon_category_id'] = $params['coupon_id'];
            ActivityModel::where(['id'=>$activity->getData('id')])->setInc('coupon_now');


            $ctid = CouponTicketsModel::insertGetId($ticket);
            if($ctid && is_numeric($ctid)){
                $data= [];
                $data['id'] = $ctid;
                $data['end_time'] = $activity->getData('end_time');
                $coupon = CouponCategoryModel::where('id','eq',$params['coupon_id'])->find();

                $data['coupon_cate_cate'] = $coupon->getData('coupon_cate_cate');
                $data['amount_cash'] = $coupon->getData('amount_cash');
                $data['discount_ticket'] = $coupon->getData('discount_ticket');
                $data['low_price'] = $coupon->getData('low_price');
                $data['name'] = $coupon->getData('name');
                throw new SuccessMessage(['msg'=>'用户成功领取优惠券','data'=>$data]);
            }else{
                throw new CouponException(['msg'=>'用户领取优惠券失败!']);
            }
        }
    }


    //验证优惠券的数量
    // 生成优惠券吗
    public function makeSequenceCode()
    {
        $str = 'abcdefghijklmnopqrstuvwxyz+-*@/ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $str = substr(str_shuffle($str),0,6).time();
        return md5($str);
    }


    //检测当前活动是否有效
    public function checkActivity($obj)
    {
        //判断活动有效的主要参考是开始,结束时间
        $now = date('Y-m-d H:i:s');
        $start_time = $obj->getData('start_time');
        $end_time = $obj->getData('end_time');

        if(($now >= $start_time) && ($now <= $end_time)){
            return true;
        }elseif($now > $end_time){
            throw new CouponException([
                'msg' => '活动已结束',
            ]);
        }else{
            throw new CouponException([
                'msg' => '活动未开始',
            ]);
        }
    }
}